<template>
    <scroll-view class="areamain">
        <select-area-item
            v-for="(item, index) in datas"
            :item="item"
            :key="index"
            @selectFn="selectFn"></select-area-item>
    </scroll-view>
</template>

<script>
import { GetCountrywideTree } from "@/service/api/web.js"
import selectAreaItem from "@/components/select/select-area-item.vue"

export default {
    components: {
        selectAreaItem,
    },
    data() {
        return {
            datas: [],
            regionData: [],
        }
    },
    onLoad(e) {
        if (e.regionData) {
            this.regionData = JSON.parse(e.regionData)
        }
    },
    mounted() {
        this.getCountrywideTree()
    },
    methods: {
        getCountrywideTree() {
            //开始层级 3-省份 4-城市 5-区县 6-街道
            let param = {
                startlev: 3,
                endlev: 4,
            }
            let path = "?startlev=3&endlev=4"
            GetCountrywideTree(param, path).then((res) => {
                console.log(res)
                if (res.State == 1) {
                    res.Data.forEach((e) => {
                        e.checked = false
                        e.Children.forEach((sub) => {
                            sub.checked = false
                        })
                    })
                    if (this.regionData) {
                        this.regionData.forEach((e, index, arr) => {
                            res.Data.forEach((res, resindex, resarr) => {
                                if (arr[index] == resarr[resindex].ID) {
                                    resarr[resindex].checked = true
                                    resarr[resindex].Children.forEach((rsp) => {
                                        rsp.checked = true
                                    })
                                }
                                res.Children.forEach((subres, subresindex, subresarr) => {
                                    if (arr[index] == subresarr[subresindex].ID) {
                                        subresarr[subresindex].checked = true
                                    }
                                })
                            })
                        })
                    }
                    this.datas = res.Data
                }
            })
        },
        selectFn(data) {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit("getSelectData", {
                data,
            })
            this.easyGoBack()
        },
        onNavigationBarButtonTap(e) {
            let selectData = []
            this.datas.forEach((e) => {
                let subnum = 0
                e.Children.forEach((sub) => {
                    if (sub.checked) {
                        subnum++
                    }
                    if (subnum == e.Children.length) {
                        let obj = {
                            ID: e.ID,
                            Name: e.Name,
                        }
                        selectData.push(obj)
                        if (e.Children.length != 1) {
                            console.log(selectData.length - e.Children.length)
                            console.log(selectData)
                            selectData.splice(selectData.length - e.Children.length, e.Children.length - 1)
                        }
                    } else if (subnum < e.Children.length && sub.checked) {
                        let obj = {
                            ID: sub.ID,
                            Name: e.Name + "-" + sub.Name,
                        }
                        selectData.push(obj)
                    }
                })
            })
            this.selectFn(selectData)
        },
    },
}
</script>

<style lang="scss" scoped>
.areamain {
    margin-top: 12rpx;
    background: #ffffff;
}
</style>
